#ifndef _SILM_LOG_H_
#define _SILM_LOG_H_

#include <stdarg.h>
#ifdef _WIN32
#include <Windows.h>
#endif//_WIN32

#define MAX_LOGFILE_SIZE    (20 * (1 << 20))//20M max size of single log file
#define MAX_LOGFILE_COUNT   10//max count of log file
#define MAX_LOGMSG_LEN      (8*1024)//8K

typedef struct _NAME_QUEUE
{
    char *logFilename[MAX_LOGFILE_COUNT];//save current log filename in last element
    unsigned char front;//queue head
    unsigned char rear;//queue tail
    unsigned char count;//element count
}NAME_QUEUE, *PNAME_QUEUE;

class CSilmLog
{

public:
    CSilmLog(void);
    ~CSilmLog(void);

    int Initialize(unsigned int bitmap, const char *filename);

    int WriteLog(unsigned int logType, const char *format, va_list args);

private:
    //list all log files, and make cache
    void ListLogFiles(void);
    //open log file
    int OpenLogFile(void);
    //check need for backup current log file
    bool CheckBackupLog(void);
    //backup current log file
    int BackupLogFile(void);

private:

    unsigned int m_bitmap;
#ifdef _WIN32
    HANDLE m_hLog;
#else
    int m_logFd;
#endif//_WIN32
    unsigned int m_filenameLen;
    unsigned int m_nameSuffixOffset;
    NAME_QUEUE m_queue;
    char m_buffer[MAX_LOGMSG_LEN];

};

#endif//_SILM_LOG_H_
